package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.ProductType;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IProductTypeService;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/productType")
public class ProductTypeController {
    @Autowired
    private IProductTypeService productTypeService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model) {
        model.addAttribute("pageInfo", productTypeService.queryProductType(qo));
        return "productType/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(ProductType entity, MultipartFile img) {
        if (img != null && img.getContentType().startsWith("image") && img.getSize() > 0) {
            String url = UploadUtil.upload(img);
            entity.setTypeUrl(url);
        }
        productTypeService.saveOrUpdate(entity);
        return "redirect:/productType/list.do";
    }
    @RequestMapping("/delete")
    public String delete(Long id) {
        productTypeService.delete(id);

        return "redirect:/productType/list.do";
    }

}
